package com.mszlu.blog.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mszlu.blog.admin.mapper.PermissionMapper;
import com.mszlu.blog.admin.model.params.PageParam;
import com.mszlu.blog.admin.pojo.Permission;
import com.mszlu.blog.admin.vo.PageResult;
import com.mszlu.blog.admin.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.krb5.internal.PAData;

import javax.annotation.Resource;


/**
 * @author Administrator
 * 权限服务
 */
@Service
public class PermissionService {

    @Resource
    private PermissionMapper permissionMapper;
    /**
     * 要的数据，管理台 表的所有的字段 Permission
     * 分页查询
     */
    public Result listPermission(PageParam pageParam) {
        //创建分页对象
        Page<Permission> page = new Page<>(pageParam.getCurrentPage(),pageParam.getPageSize());
        //查询条件
        LambdaQueryWrapper<Permission> queryWrapper = new LambdaQueryWrapper<>();
        //查询条件
        if (StringUtils.isNotBlank(pageParam.getQueryString())){
            //查询权限名称对应的  搜索字段
            queryWrapper.eq(Permission::getName,pageParam.getQueryString());
        }
        //获取Permission 分页结果集
        Page<Permission> permissionPage = permissionMapper.selectPage(page, queryWrapper);
        //创建一个page响应对象
        PageResult<Permission> pageResult = new PageResult<>();
        //结果集
        pageResult.setList(permissionPage.getRecords());
        //总条数
        pageResult.setTotal(permissionPage.getTotal());
        //返回pageResult对象
        return Result.success(pageResult);
    }


    /**
     * 新增
     * @param permission 权限信息
     * @return success
     */
    public Result add(Permission permission) {
        this.permissionMapper.insert(permission);
        return Result.success(null);
    }



    /**
     * 修改
     * @param permission 权限信息
     * @return success
     */
    public Result update(Permission permission) {
        this.permissionMapper.updateById(permission);
        return Result.success(null);
    }


    /**
     * 删除
     * @param id 授权id
     * @return success
     */
    public Result delete(Long id) {
        this.permissionMapper.deleteById(id);
        return Result.success(null);
    }
}
